home *** CD-ROM | disk | FTP | other *** search
- From: Philippe Verdy <100105.3120@compuserve.com>
- Message-ID: <4jsk6r$t2m@dub-news-svc-3.compuserve.com>
- X-Original-Date: 3 Apr 1996 01:27:55 GMT
- Path: in1.uu.net!bounce-back
- Date: 03 Apr 96 03:13:06 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Interest of static constructors, semantics, examples
- Organization: CompuServe Incorporated
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMWHs1OEDnX0m9pzZAQHAAQF+KP5XtLs1w1u+Tx4UktSGHcer2KAn9rlT
- uCzl5RcbNKz5blP/xBe8blqQR+s+DYgy
- =mVRw
-
- I should first state the semantic and rules of static
- constructor before proceeding further with static inheritance
-
- - If a class defines a static constructor, its static members
- must not be initialized. Only the whole class can be
- initialized with the following syntax:
- static class X;
- // uses the static default constructor
- or:
- static class X(parameters);
- // uses a parameterized static constructor.
- // Optionally supported, because it adds syntax conflicts
- or:
- static (class X(parameters));
- // to solve the ambiguities
-
- - A static constructor initialize its static members the
- same way an instance constructor calls the constructors
- for each of their non-static members:
- static X::X()
- : static_member1(value1)
- , static_member2(value2)
- {
- // additional code
- };
-
- - All static members not described in this list must have
- a default constructor defined, which will be implicitly
- called in declaration order after each explicit call of
- static member constructors.
-
- - A static constructor can call the standard constructor
- of a class from which it derives statically:
- class Y { ... };
- class X : static Y
- {
- static X()
- : Y() // calls the standard instance constructor of Y
- , classmember1(value1)
- { ... }
- X() {...} // standard default constructor for instances
- };
-
- - If a static base class Y (we will call it a metaclass) is
- not initialized by calling one of its constructors from the
- static constructor of the class, the metaclass Y must have
- a default constructor which will be called implicitly
- in the order of declaration of the metaclasses, but after
- the other explicit metaclass constructions
-
- - The static members and metaclasses destructors will be
- implicitly called in the reverse order of the calls of
- constructors.
-
- - There should only be one static constructor for any class,
- because this class construction will occur only once for
- the whole application.
-
- - Classes may have static base classes, called metaclasses,
- in addition to their static members. This means that the
- class inherits statically of a unique instance of its
- metaclass. The methods which apply on instances of these
- base classes will apply to the class itself, even without
- specifying any of its instances.
- The access rules (public, protected, private) for these
- inherited properties (attributes and methods) are the same
- as for standard (non-static) base classes properties.
- Without its additional methods and members, the class
- can be seen as a static variable, constructed with the
- code implemented in the static constructor.
-
- - Multiple static inheritance is possible. In fact, its like
- if we built an anonymous class which inherits non statically
- from the same bases, and used it to build a classic (but
- anonymous) instance, which properties would be fully
- accessible within our class. The rules for the construction
- of this instance are the same as the rule of constuction
- of a static instance of a standard class which has multiple
- inheritance.
-
- - static virtual inheritance is implied for the same reasons.
- This is to solve the conflicts which come from multiple
- inheritance, the same way we did for non-static inheritance.
-
- - static functions can be made virtual. This will most often
- occur if one of the static base classes define non-static
- virtual methods, so that we can override them within our
- class, while being able to call them directly from the
- base class.
-
- - There is no problem of multiple base class constructions,
- because we don't call directly the static base class
- constructor. Instead, the compiler generates at run-time
- a virtual table by using a flag in each class vtable which
- will also be used to chain the classes constructed, so that
- destructors are called in the appropriate order.
-
- Now here are the interest of static constructors:
-
- - You can create a metaclass/class hierarchy which fixes the
- order of construction of each classes.
- - You can use multiple inheritance of containers to create
- custom class registration with much more power than RTTI.
- - Using this feature, you avoid most of templates, and
- still have more compact code, while allowing for dynamic
- registration of classes at run-time (for example when
- loading additional classes from a dynamically loaded DLL
- or driver). We can use virtual static function pointers as
- arguments to the constructors of static baseclasses.
-
- Example:
-
- class RegisterClass {
- public:
- class MyClass {
- public:
- MyClass(RegisterClass *cl, const char *name) {
- C = cl;
- N = nm;
- }
- RegisterClass *C;
- char *N;
- }
-
- RegisterClass(char *name)
- {
- reg.insert(new MyClass(this, name));
- }
-
- private:
- static Array<MyClass * const> reg;
- }
-
- class Y : virtual static RegisterClass
- {
- static Y()
- : RegisterClass("Y")
- {
- // some code to do with constructed static members of Y
- // make Y usable to create instances
- };
- ...
- }
-
- class X : Y, virtual static RegisterClass
- {
- static X()
- : Y
- , RegisterClass("X")
- , BaseClass("Y")
- {
- // some code to do with constructed static members of X
- // make X usable to create instances
- }
- ...
- }
-
- class Z : virtual X, virtual Y, virtual static RegisterClass
- {
- static Z()
- : X(), Y()
- , RegisterClass("Z")
- , BaseClass("X")
- , BaseClass("Y")
- {
- // some code to do with constructed static members of Z
- // make Z usable to create instances
- }
- ...
- }
-
- -
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-